package cn.lunkr.sdkdemo;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import cn.lunkr.sdkdemo.databinding.ActivitySettingsBinding;
import cn.lunkr.sdkdemo.util.SPUtil;

public class SettingsActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ActivitySettingsBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        boolean isSSO = "sso".equals(SPUtil.getInstance().getString(SPUtil.LOGIN_TYPE, ""));

        binding.loginTypeRg.check(isSSO ? R.id.sso_login_rb : R.id.account_login_rb);
        binding.appsid.setVisibility(isSSO ? View.VISIBLE : View.GONE);

        binding.loginTypeRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.account_login_rb) {
                    // 帐号密码登录
                    SPUtil.getInstance().saveString(SPUtil.LOGIN_TYPE, "account");
                    binding.appsid.setVisibility(View.GONE);
                    Toast.makeText(SettingsActivity.this, "输入URL保存后，重启App生效", Toast.LENGTH_LONG).show();
                } else {
                    // 单点登录
                    SPUtil.getInstance().saveString(SPUtil.LOGIN_TYPE, "sso");
                    binding.appsid.setVisibility(View.VISIBLE);
                    Toast.makeText(SettingsActivity.this, "输入URL appSid, 保存后，重启App生效", Toast.LENGTH_LONG).show();
                }

            }
        });

        SPUtil spUtil = SPUtil.getInstance();
        String url = spUtil.getString(SPUtil.URL, "");
        if (!TextUtils.isEmpty(url)) {
            binding.url.setText(url);
        }
        binding.appsid.setText(spUtil.getString(SPUtil.TOKEN, ""));

        binding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(binding.url.getText().toString().trim())) {
                    Toast.makeText(SettingsActivity.this, "请输入coremail服务器URL", Toast.LENGTH_SHORT).show();
                    return;
                }
                SPUtil spUtil = SPUtil.getInstance();
                spUtil.saveString(SPUtil.URL, binding.url.getText().toString().trim());

                if (binding.loginTypeRg.getCheckedRadioButtonId() == R.id.sso_login_rb
                        && TextUtils.isEmpty(binding.appsid.getText().toString().trim())) {
                    Toast.makeText(SettingsActivity.this, "请输入 appsid", Toast.LENGTH_SHORT).show();
                    return;
                }
                spUtil.saveString(SPUtil.TOKEN, binding.appsid.getText().toString().trim());
                Toast.makeText(SettingsActivity.this, "已保存", Toast.LENGTH_SHORT).show();
                SettingsActivity.this.finish();
            }
        });
    }
}